﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Utilities;
using SpringWaterDataAccess;

namespace sow
{
	/// <summary>
	/// Summary description for shujia.
	/// </summary>
	public partial class shujia : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlImage img37;
		protected System.Web.UI.HtmlControls.HtmlImage img31;
		protected System.Web.UI.HtmlControls.HtmlImage img32;
		protected System.Web.UI.HtmlControls.HtmlImage img38;
		protected System.Web.UI.HtmlControls.HtmlImage img34;
		protected System.Web.UI.HtmlControls.HtmlImage img36;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack) 
			{
				//Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
				BindDataForRepeater();
									
			}
		}
		
		private void BindDataForRepeater()
		{
			HttpCookie emailCookie = Request.Cookies["email"];

			// Read the cookie information and display it.
			if (emailCookie == null) 
			{
				Response.Redirect("index.aspx?message=请您先注册/登录，再使用'我的书架'存放和管理你所喜爱的文章! ");
			} 

			DataSet ds = UserObject.GetWordsOnMyBookshelf(emailCookie.Value);
			
			if (ds.Tables[0].Rows[0].IsNull("Title")) 
			{
				ds.Clear();
			}
				
			MyRepeater.DataSource = ds.Tables[0].DefaultView;
			MyRepeater.DataBind();
				
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void Button2_Click(object sender, System.EventArgs e)
		{
			
			foreach (RepeaterItem myitem in MyRepeater.Items) 
			{
				
				foreach (Control c in myitem.Controls) 
				{
					if (c is CheckBox ) 
					{ 
						CheckBox cb = (CheckBox)c;
											
						if ((cb.Checked)) 
						{
							
							// Each row has a invisible Label for dispalying word ID
							Label idLabel = (Label)myitem.FindControl("Label1");

							if (idLabel != null) 
							{
								HttpCookie emailCookie = Request.Cookies["email"];

								// Read the cookie information
								if (emailCookie == null) 
								{
									Response.Redirect("index.aspx?message=请您在泉源首页登录");
									//MessageBox.Show("请您去泉源首页登录");
								} 
								
								UserObject.RemoveWordFromShelf(Convert.ToInt32(idLabel.Text), 
									            emailCookie.Value); 

							}
						}
					}
					
				}
			}

			// Refresh data from database
			BindDataForRepeater();
		}

		protected void Button1_Click(object sender, System.EventArgs e)
		{
			Button2_Click(sender, e);
		}
	}
}
